//
//  FindVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/12.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class FindVc: UIViewController {
    let RecordRecommendTableViewCellID = "RecordRecommendTableViewCell"
    var commendModelArr:[RecordCommendModel] = [RecordCommendModel]()
    var programId:Int = 0
    private lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - OFFSET_BOTTOM), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(RecordRecommendTableViewCellID), forCellReuseIdentifier: RecordRecommendTableViewCellID)
        tableView.tableHeaderView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 0))
        
        return tableView
    }()
    
    var headView:FindHeadView = {
        let head = FindHeadView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 35))
        head.tapBlock = { () in
            let mainVC = TTTabBarController()
            AppDelegate.getAppDelegate().window?.rootViewController = mainVC
            mainVC.selectedIndex = 2
        }
        head.isHidden = true
        return head
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "找相似"
        // Do any additional setup after loading the view.
        setTop()
    
        setTableView()
        setCommendData()
        self.view.addSubview(self.headView)
    
    }
}

//  MARK: - ###### 方法 ######
extension FindVc {
    func setTop() {
        self.view.addSubview(headView)
    }
    
    func setTableView() {
        self.view.addSubview(self.tableView)
    }
    
    func hiddenHead() {
        UIView.animate(withDuration: 2) {
            self.headView.isHidden = true
        }
    }
    
    func showHead() {
        UIView.animate(withDuration: 1) {
            self.headView.isHidden = false
            
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4) {
            self.hiddenHead()
        }
    }
}

//  MARK: - ###### 方法 ######
extension FindVc {
    func setCommendData() {
        MTProgressHUD.show()
        RecordRequestModel.loadConformData(parameters: ["userId":String.getUserId(), "categoryId":self.programId]) { (success, message, model) in
            if success && model != nil {
                self.commendModelArr = model!
            }
            self.tableView.reloadData()
            if self.commendModelArr.count == 0 {
                self.tableView.setNoData(nodata: true)
            } else {
                self.tableView.setNoData(nodata: false)
            }
            MTProgressHUD.hidded(true)
        }
    }
    
    @objc func moreBtnAction() {
        let mainVC = TTTabBarController()
        AppDelegate.getAppDelegate().window?.rootViewController = mainVC
        mainVC.selectedIndex = 0
    }
    
    func toCollect(programId:Int) {
        HomeRequestModel.increaseHistoryAndCollections(parameters: ["userId":String.getUserId(),"status":2,"programId":programId,"h5Url":""]) { (success) in
            if success {
                self.setCommendData()
                TTAlertView.showMessage(message: "收藏成功")
            }
            
        }
    }
    func noCollect(programId:Int) {
        HomeRequestModel.delHistory(parameters: ["userId":String.getUserId(),"programId":programId,"h5Url":""]) { (success) in
            if success {
                self.setCommendData()
                TTAlertView.showMessage(message: "取消收藏")
            }
            
        }
    }
}

//  MARK: - ###### 协议 ######
extension FindVc: UITableViewDelegate, UITableViewDataSource, RecordRecommendTableViewCellDelegate {
    func collectActionDel(cell: UITableViewCell) {
        let index:IndexPath = self.tableView.indexPath(for: cell) as! IndexPath
        let model = self.commendModelArr[index.row]
        self.showHead()
        if model.markStatus == 0 {
            self.noCollect(programId: model.id)
        } else {
            self.toCollect(programId: model.id)
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return commendModelArr.count
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 94
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: RecordRecommendTableViewCellID) as! RecordRecommendTableViewCell
        cell.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
        let model = self.commendModelArr[indexPath.row]
        cell.delegate = self
        cell.leftImageView.sd_setImage(with: URL(string: StringIncludeChineseToString(model.shareImageUrl ?? "")), completed: nil)
        cell.titleLabel.text = model.title
        cell.desLabel.text = model.describe
        cell.newLabel.text = "\(model.totalSeason)集更新"
        cell.listenLabel.text = "\(model.listenNum)"
        cell.tolLabel.text = "\(model.totalSeason)集"
        if model.markStatus == 0 {
            cell.collectBtn.setTitleColor(RGB(230, 67, 64), for: .normal)
            cell.collectImage.image = IMAGE(name: "record已收藏")
        } else {
            cell.collectBtn.setTitleColor(RGB(130, 136, 136), for: .normal)
            cell.collectImage.image = IMAGE(name: "record收藏")
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.commendModelArr[indexPath.row]
        let playVc = PlayVC()
        playVc.id = model.id
        PUSHVIEWCONTROLLER(playVc)
    }
}

